/*=============================================================================================================
 * ViewFragment.java
 *
 * 1.프로젝트 : ToYou - 명함관리 어플
 * 2.패 키 지 : com.trillion.toyou
 * 3.작업환경 : Android
 * 2.작 업 자 : 손호준
 * 3.작업일자 : 2014.12.09
 * 4.내    용 : 명함 내용을 출력하는 프래그먼트 화면
 * 5.수 정 자 :
 * (1) 손호준
 * 
 * 6.수정일자 :
 * (1) 2014.12.12 05:01pm
 * 
 * 7.수정내용 :
 * (1) CreateView 시 나오는 Toast 삭제
 * 
 * =============================================================================================================*/
package com.trillion.toyou;

import com.trillion.toyou.R.id;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ViewFragment extends Fragment implements OnClickListener{
	// 뷰 지정
	View view = null;
	
	// 프래그먼트 관련
	Fragment frag = null;
	FragmentTransaction trans = null;
	Bundle bundle = null;
	
	String nid = null;
	
	DBHelper db = null;
	Data data = null;
	
	LinearLayout topTitle = null;
	
	
	Button categoryTiltle = null;
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.view = inflater.inflate(R.layout.view_layout, container, false);
		
		MainActivity.page_check = new MainActivity().BACK_LIST;
		
		bundle = getArguments();
		
		ImageButton btnBack = (ImageButton)this.view.findViewById(R.id.viewLayout_imgBtnBack);
		
		
		ImageButton btnPhoneCall = (ImageButton)this.view.findViewById(R.id.view_layout_btn_phonecall);
		ImageButton btnModify = (ImageButton)this.view.findViewById(R.id.view_layout_btn_modify);
		ImageButton btnDelete = (ImageButton)this.view.findViewById(R.id.view_layout_btn_delete);
		
		
		this.topTitle = (LinearLayout)this.view.findViewById(R.id.view_layout_top);
		
		
		
		
		
		
		
		categoryTiltle = (Button)this.view.findViewById(R.id.viewlayout_handle);
		
		
		
		btnBack.setOnClickListener(this);
		btnPhoneCall.setOnClickListener(this);
		btnModify.setOnClickListener(this);
		btnDelete.setOnClickListener(this);
		
		
		
		viewview(bundle);
		
		return view;
	}
	
	@Override
	public void onResume() {
		new NfcOnOffChange(this.topTitle, R.drawable.top_bar_on_nfc_get, R.drawable.top_bar_off_nfc_get);
		super.onResume();
	}
	
	
	
	public void viewview(Bundle bundle)
	{
		this.db = new DBHelper(getActivity(), "toyou", null, 1);
		this.data = new Data();

		TextView nname = (TextView)view.findViewById(R.id.view_layout_tv_nname);
		TextView grade = (TextView)view.findViewById(R.id.view_layout_tv_grade);
		TextView company = (TextView)view.findViewById(R.id.view_layout_tv_company);
		TextView department= (TextView)view.findViewById(R.id.view_layout_tv_department);
		TextView address = (TextView)view.findViewById(R.id.view_layout_tv_address);
		
		TextView email= (TextView)view.findViewById(R.id.view_layout_tv_email);
		TextView telNum = (TextView)view.findViewById(R.id.view_layout_tv_telNum);
		TextView phoneNum = (TextView)view.findViewById(R.id.view_layout_tv_phoneNum);
		
		
		this.nid = getArguments().get("id").toString();
		data = db.selectone(this.nid);
		
		nname.setText(data.getNname().toString());
		grade.setText(data.getGrade().toString());
		company.setText(data.getCompany().toString());
		department.setText(data.getDepartment().toString());
		address.setText(data.getAddress().toString());
		
		email.setText(data.getEmail().toString());
		telNum.setText(data.getTelNum().toString());
		phoneNum.setText(data.getPhoneNum().toString());
		
		
		this.categoryTiltle.setTextColor(Color.WHITE);
		
		if(this.data.getCategory().equals("vip")){
			this.categoryTiltle.setText("그룹 : VIP");
			
		}else if(this.data.getCategory().equals("company")){
			this.categoryTiltle.setText("그룹 : 회사");
			
		}else if(this.data.getCategory().equals("friends")){
			this.categoryTiltle.setText("그룹 : 지인");
			
		}else if(this.data.getCategory().equals("other")){
			this.categoryTiltle.setText("그룹 : 기타");
			
		}

		
		
		
		telNum.setOnClickListener(this);
		phoneNum.setOnClickListener(this);

		
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		this.trans = getFragmentManager().beginTransaction();
		AlertDialog.Builder ad = new AlertDialog.Builder(getActivity());
		
		Uri uri = null;
		Intent intent = null;
		
		switch (id) {

			
			
			
			
		case R.id.viewLayout_imgBtnBack:
			this.frag = new ListFragment();
			
			this.trans.replace(R.id.root_layout, this.frag);
			this.trans.addToBackStack(null);
			this.trans.commitAllowingStateLoss();
			
			break;
			
		case R.id.view_layout_tv_phoneNum:
			ad.setTitle(this.data.getNname() +" " + this.data.getGrade() + "님께 전화");
			ad.setMessage(this.data.getPhoneNum() + " 번호로\n전화를 하시겠습니까?");
			
			ad.setNegativeButton("확인", 
					new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Uri uri = Uri.parse("tel:" + data.getPhoneNum());
							
							Intent intent = new Intent(Intent.ACTION_CALL, uri);
							startActivity(intent);
						}
					});
		
			
			ad.setPositiveButton("취소", null);
			
			ad.show();

			break;
		
			
			
		case R.id.view_layout_tv_telNum:
			ad.setTitle(this.data.getNname() +" " + this.data.getGrade() + "님께 전화");
			ad.setMessage(this.data.getTelNum() + " 번호로\n전화를 하시겠습니까?");
			
			ad.setNegativeButton("확인", 
					new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Uri uri = Uri.parse("tel:" + data.getTelNum());
							
							Intent intent = new Intent(Intent.ACTION_CALL, uri);
							startActivity(intent);
						}
					});
		
			
			ad.setPositiveButton("취소", null);
			
			ad.show();
			
			break;
		
			
			
			
			

		
			
			
		
		case R.id.view_layout_btn_phonecall:
			
			final CharSequence[] ITEMS = {"T : " + this.data.getTelNum(), "M : "+this.data.getPhoneNum()};
			
			ad.setTitle(this.data.getNname() + " " + this.data.getGrade() + "님께 전화");
			ad.setItems(ITEMS, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					String  temp = ITEMS[which].toString();
					String[] phoneNum = temp.split(" : ");
					Uri uri = Uri.parse("tel:" + phoneNum[1]);
					
					Intent intent = new Intent(Intent.ACTION_CALL, uri);
					startActivity(intent);
				}
			});
			ad.setNegativeButton("취소", null);
			ad.show();
			
			
			
			break;	
			
			
			
			
		case R.id.view_layout_btn_modify:
			this.bundle = new Bundle();
			this.bundle.putString("id", this.nid);
			
			
			this.frag = new ModifyOtherCategoryFragment();
			this.frag.setArguments(this.bundle);
			
			
			this.trans.replace(R.id.root_layout, this.frag);
			this.trans.addToBackStack(null);
			this.trans.commitAllowingStateLoss();
			
			break;
		
			
			
			
			
		case R.id.view_layout_btn_delete:
			
			ad.setTitle("명함 삭제");
			ad.setMessage(this.data.getNname() +" " + this.data.getGrade() + "님의 명함을\n삭제하시겠습니까?");
			
			ad.setNegativeButton("확인", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					boolean b = db.delete(data.getNid());
					
					
					if(b){
						frag = new ListFragment();
						
						trans.replace(R.id.root_layout, frag);
						trans.addToBackStack(null);
						trans.commitAllowingStateLoss();
						Toast.makeText(getActivity(), "정상적으로 삭제되었습니다.", Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(getActivity(), "삭제 중 오류가 발생했습니다.", Toast.LENGTH_SHORT).show();	
					}
					
				}
			});
			
			ad.setPositiveButton("취소", null);
			
			
			ad.show();
			
			break;
			

		}
		
		
	}
}
